//
//  ModuleView.m
//  Game2048
//
//  Created by mis on 16/3/24.
//  Copyright © 2016年 mis. All rights reserved.
//

#import "ModuleView.h"

@implementation ModuleView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        moduleWidth = (self.frame.size.width - 60) / 4;
        [self initBackground];
    }
    return self;
}

- (void)initBackground {
    for (UInt8 row = 0; row < 4; row++) {
        for (UInt8 col = 0; col < 4; col++) {
            [self addSubview:[self getModuleRow:row Col:col]];
        }
    }
}

- (UILabel *)getModuleRow:(UInt8)row Col:(UInt8)col {
    UILabel *module = [[UILabel alloc] initWithFrame:CGRectMake(15 + (moduleWidth + 10) * col, 15 + (moduleWidth + 10) * row, moduleWidth, moduleWidth)];
    module.clipsToBounds = YES;
    module.layer.cornerRadius = 6;
    module.backgroundColor = [UIColor whiteColor];
    return module;
}

- (NSMutableArray *)moduleArray {
    if (!_moduleArray) {
        _moduleArray = [NSMutableArray array];
        for (UInt8 row = 0; row < 4; row++) {
            NSMutableArray *modules = [NSMutableArray array];
            for (UInt8 col = 0; col < 4; col++) {
                UILabel *module = [self getModuleRow:row Col:col];
                module.textAlignment = NSTextAlignmentCenter;
                module.textColor = [UIColor whiteColor];
                module.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:20];
                [modules addObject:module];
            }
            [_moduleArray addObject:modules];
        }
    }
    return _moduleArray;
}

- (void)drawRect:(CGRect)rect {
    for (UInt8 row = 0; row < 4; row++) {
        for (UInt8 col = 0; col < 4; col++) {
            UILabel *module = self.moduleArray[row][col];
            module.backgroundColor = [self getModuleColor:self.moduleData[row][col]];
            module.text = [NSString stringWithFormat:@"%hu",self.moduleData[row][col]];
            if(self.moduleData[row][col]) {
                module.text = [NSString stringWithFormat:@"%hu",self.moduleData[row][col]];
            }
            [self addSubview:module];
        }
    }
}

- (UIColor *)getModuleColor:(UInt16)data {
    UIColor *moduleColor = nil;
    switch (data) {
        case 0:
            moduleColor = [[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:0];
            break;
        case 2:
            moduleColor = [self getColorRed:0xff Green:0xcc Blue:0x33];
            break;
        case 4:
            moduleColor = [self getColorRed:0xff Green:0xcc Blue:0x00];
            break;
        case 8:
            moduleColor = [self getColorRed:0xff Green:0x99 Blue:0x33];
            break;
        case 16:
            moduleColor = [self getColorRed:0xff Green:0x99 Blue:0x00];
            break;
        case 32:
            moduleColor = [self getColorRed:0xcc Green:0xcc Blue:0x33];
            break;
        case 64:
            moduleColor = [self getColorRed:0xcc Green:0x99 Blue:0xcc];
            break;
        case 128:
            moduleColor = [self getColorRed:0xcc Green:0x99 Blue:0x33];
            break;
        case 256:
            moduleColor = [self getColorRed:0xcc Green:0x66 Blue:0x00];
            break;
        case 512:
            moduleColor = [self getColorRed:0x99 Green:0x99 Blue:0x33];
            break;
        case 1024:
            moduleColor = [self getColorRed:0x99 Green:0x99 Blue:0x00];
            break;
        case 2048:
            moduleColor = [self getColorRed:0x33 Green:0xcc Blue:0x33];
            break;
        case 4096:
            moduleColor = [self getColorRed:0x33 Green:0x99 Blue:0x33];
            break;
        case 8192:
            moduleColor = [self getColorRed:0x33 Green:0x33 Blue:0x33];
            break;
        default:
            break;
    }
    return moduleColor;
}

- (UIColor *)getColorRed:(UInt8) red Green:(UInt8)green Blue:(UInt8)blue {
    return [UIColor colorWithRed:red / 255.0 green:green / 255.0 blue:blue / 255.0 alpha:1.0];
}


- (UIColor *)getModuleColor2:(UInt32)data {
    UInt8 red = data >> 16 & 0xff;
    UInt8 green = data >> 8 & 0xff;
    UInt8 blue = data & 0xff;
    return [UIColor colorWithRed:red / 255.0 green:green / 255.0 blue:blue / 255.0 alpha:1.0];

}

@end
